package cloud.shyidong.admin.service.impl;

import cloud.shyidong.admin.service.SelfService;
import cloud.shyidong.cache.service.CacheService;
import cloud.shyidong.core.Result;
import cloud.shyidong.core.constant.TokenConstant;
import cloud.shyidong.core.context.ContextHolder;
import cloud.shyidong.core.exception.BusinessException;
import cloud.shyidong.core.util.DateUtils;
import cloud.shyidong.core.util.MD5Utils;
import cloud.shyidong.core.util.ObjectUtils;
import cloud.shyidong.core.util.StringUtils;
import cloud.shyidong.system.dao.*;
import cloud.shyidong.system.entity.SystemCacheEntity;
import cloud.shyidong.system.entity.SystemUserEntity;
import cloud.shyidong.system.request.InfoUpdatePasswordRequest;
import cloud.shyidong.system.request.InfoUpdateRequest;
import com.alibaba.fastjson.JSON;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

/**
 * @Description TODO
 * @Author syd
 * @Date 2023/6/1
 */
@Service
public class SelfServiceImpl implements SelfService {
    
    private SystemUserDao systemUserDao;
    
    private SystemMenuDao systemMenuDao;
    
    private SystemFunctionDao systemFunctionDao;
    
    private SystemUserRoleDao systemUserRoleDao;
    
    private SystemRoleMenuDao systemRoleMenuDao;
    
    private SystemRoleFunctionDao systemRoleFunctionDao;
    
    private CacheService cacheService;
    
    @Autowired
    public void setSystemUserDao(SystemUserDao systemUserDao) {
        this.systemUserDao = systemUserDao;
    }
    
    @Autowired
    public void setSystemMenuDao(SystemMenuDao systemMenuDao) {
        this.systemMenuDao = systemMenuDao;
    }
    
    @Autowired
    public void setSystemFunctionDao(SystemFunctionDao systemFunctionDao) {
        this.systemFunctionDao = systemFunctionDao;
    }
    
    @Autowired
    public void setSystemUserRoleDao(SystemUserRoleDao systemUserRoleDao) {
        this.systemUserRoleDao = systemUserRoleDao;
    }
    
    @Autowired
    public void setSystemRoleMenuDao(SystemRoleMenuDao systemRoleMenuDao) {
        this.systemRoleMenuDao = systemRoleMenuDao;
    }
    
    @Autowired
    public void setSystemRoleFunctionDao(SystemRoleFunctionDao systemRoleFunctionDao) {
        this.systemRoleFunctionDao = systemRoleFunctionDao;
    }
    
    @Autowired
    public void setCacheService(CacheService cacheService) {
        this.cacheService = cacheService;
    }
    
    @Override
    public Result get() {
        
        if (StringUtils.isEmpty(ContextHolder.getToken())) {
            return Result.ofErrors(BusinessException.noLogin());
        }
        
        SystemCacheEntity systemCacheEntity = cacheService.get(String.format(TokenConstant.TOKEN_USERINFO_KEY, ContextHolder.getToken()));
        if (ObjectUtils.isNull(systemCacheEntity)) {
            return Result.ofErrors(BusinessException.noLogin());
        }
        
        return Result.ofData(systemCacheEntity.getValue());
    }
    
    @Override
    @Transactional(rollbackFor = Exception.class)
    public Result update(InfoUpdateRequest infoUpdateRequest) {
        
        //查询登录用户信息
        SystemUserEntity systemUser = systemUserDao.selectById(ContextHolder.getContext().getUserId());
        
        //修改登录用户信息
        if (StringUtils.isNotEmpty(infoUpdateRequest.getName())) {
            systemUser.setName(infoUpdateRequest.getName());
        }
        if (StringUtils.isNotEmpty(infoUpdateRequest.getPhone())) {
            systemUser.setPhone(infoUpdateRequest.getPhone());
        }
        if (StringUtils.isNotEmpty(infoUpdateRequest.getEmail())) {
            systemUser.setEmail(infoUpdateRequest.getEmail());
        }
        if (StringUtils.isNotEmpty(infoUpdateRequest.getRemark())) {
            systemUser.setRemark(infoUpdateRequest.getRemark());
        }
        systemUser.setUpdateUser(ContextHolder.getContext().getUserId());
        systemUser.setUpdateTime(DateUtils.getNow());
        systemUserDao.updateById(systemUser);
        
        systemUser.setPassword(null);
        cacheService.save(String.format(TokenConstant.TOKEN_USERINFO_KEY, ContextHolder.getToken()), JSON.toJSONString(systemUser));
        
        return Result.ofData(null);
    }
    
    @Override
    @Transactional(rollbackFor = Exception.class)
    public Result updatePassword(InfoUpdatePasswordRequest infoUpdatePasswordRequest) {
        
        //修改登录用户信息
        SystemUserEntity systemUser = systemUserDao.selectById(ContextHolder.getContext().getUserId());
        
        //验证旧密码是否正确
        if (!systemUser.getPassword().equals(MD5Utils.md5Hex(infoUpdatePasswordRequest.getOldPassword()))) {
            throw BusinessException.from("旧密码输入错误");
        }
        
        //修改密码
        systemUser.setPassword(MD5Utils.md5Hex(infoUpdatePasswordRequest.getNewPassword()));
        systemUser.setUpdateUser(ContextHolder.getContext().getUserId());
        systemUser.setUpdateTime(DateUtils.getNow());
        systemUserDao.updateById(systemUser);
        
        return Result.ofData(null);
    }
    
    @Override
    public Result logout() {
        
        if (StringUtils.isNotEmpty(ContextHolder.getToken())) {
            cacheService.delete(String.format(TokenConstant.TOKEN_USERINFO_KEY, ContextHolder.getToken()));
            cacheService.delete(String.format(TokenConstant.TOKEN_MENU_KEY, ContextHolder.getToken()));
            cacheService.delete(String.format(TokenConstant.TOKEN_FUNCTION_KEY, ContextHolder.getToken()));
            cacheService.delete(String.format(TokenConstant.TOKEN_MENU_TREE_KEY, ContextHolder.getToken()));
        }
        
        return Result.ofData(null);
    }
    
    @Override
    public Result getTreeList() {
        
        SystemCacheEntity systemCacheEntity = cacheService.get(String.format(TokenConstant.TOKEN_MENU_TREE_KEY, ContextHolder.getToken()));
        return Result.ofData(systemCacheEntity.getValue());
        
    }
    
}
